//
//  MoreWorkmateVC.m
//  MyProject
//
//  Created by 石小强 on 2018/3/23.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "MoreWorkmateVC.h"
#import "MoreWorkmateCell.h"
#import "WorkmateModel.h"

@interface MoreWorkmateVC ()

@property (nonatomic, strong) NSMutableArray *workmateArray;

@end

@implementation MoreWorkmateVC {
    NSInteger _pageNo;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"更多在职同事";
    self.view.backgroundColor = kStyleColor;
    self.tableView.backgroundColor = kStyleColor;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MoreWorkmateCell class]) bundle:nil] forCellReuseIdentifier:@"MoreWorkmateCell"];
    
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pageNo = 1;
        [self loadWorkmateData];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _pageNo += 1;
        [self loadWorkmateData];
    }];
    
    [self.tableView.mj_header beginRefreshing];
}

- (void)loadWorkmateData {
    WS(weakSelf)
    [SendRequest getCompanyIncumbentListByCompanyId:self.companyId andPageNo:[NSString stringWithFormat:@"%zd", _pageNo] result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"获取在职同事数据 = %@", [result objectForKey:@"data"]);
            NSArray *datas = [WorkmateModel mj_objectArrayWithKeyValuesArray:[[result objectForKey:@"data"] objectForKey:@"list"]];
            
            if (_pageNo == 1) {
                [weakSelf.workmateArray removeAllObjects];
                [weakSelf.workmateArray addObjectsFromArray:datas];
                [weakSelf.tableView reloadData];
                [weakSelf.tableView.mj_header endRefreshing];
                [weakSelf.tableView.mj_footer endRefreshing];
            } else {
                [weakSelf.workmateArray addObjectsFromArray:datas];
                [weakSelf.tableView reloadData];
                if (datas.count < kNumOfPageIntSize) {
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                } else {
                    [weakSelf.tableView.mj_footer endRefreshing];
                }
            }
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.workmateArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 116;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MoreWorkmateCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MoreWorkmateCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = [self.workmateArray objectAtIndex:indexPath.row];
    return cell;
}

- (NSMutableArray *)workmateArray {
    if (!_workmateArray) {
        _workmateArray = [NSMutableArray array];
    }
    return _workmateArray;
}


@end
